#!/usr/bin/make -f

default: help

package?=tizen-helper
profile?=common
arch?=$(shell arch)
tmp?=._$(shell date -u +%s).tmp

profiles?=$(shell grep '\[profile' config/gbs.conf \
 | sed -e 's|\[\(profile\..*\)\]|\1|g' \
)

urls?=$(shell grep 'url=' config/gbs.conf \
 | sed -e 's|url=\(.*\)|\1|g' \
)

SELF?=$(lastword $(MAKEFILE_LIST))
MAKE=make -f ${SELF}

help: README.txt
	cat $<
	@echo "Existing profiles:"
	@echo "${profiles}" | tr ' ' '\n'

clean:
	rm -f *~ *.tmp

install: config/gbs.conf
	install -d ${DESTDIR}/usr/share/${package}/
	install $< ${DESTDIR}/usr/share/${package}/

deploy: config/gbs.conf
	mv -v ${HOME}/.gbs.conf ${HOME}/.gbs.conf${tmp}
	ln -fs ${CURDIR}/$< ${HOME}/.gbs.conf

gbs/%:
	gbs ${@F} -P ${profile} --arch ${arch} --include-all

profile.%-x86_64:
	${MAKE} gbs/build profile=${@F} arch=x86_64

profile.%-i586:
	${MAKE} gbs/build profile=${@F} arch=i586

profile.%-armv6l:
	${MAKE} gbs/build profile=${@F} arch=armv6l

profile.%-armv7hl:
	${MAKE} gbs/build profile=${@F} arch=armv7hl

profile.%-armv7l:
	${MAKE} gbs/build profile=${@F} arch=armv7l

profile.%-aarch64:
	${MAKE} gbs/build profile=${@F} arch=aarch64

profile.%-mips:
	${MAKE} gbs/build profile=${@F} arch=mips

profile.%-mipsel:
	${MAKE} gbs/build profile=${@F} arch=mipsel

check/profile/%:
	${MAKE} ${@F} | tee ${@D}-${@F}.tmp 
	rm -f ${@D}-${@F}.tmp 

check/urls:
	for url in ${urls} ; do \
	wget -O /dev/null $${url}  >/dev/null 2>&1 \
	|| echo "error: $${url}" ; done

check: clean check/urls
	for profile in ${profiles} ; do \
  ${MAKE} check/profile/$${profile} || echo "error: ${profile}"; \
 done
	@echo "Failed:"
	ls ${@D}-*.tmp && exit 1 || echo "NONE"

.PHONY: default help
